
package com.scs.application.modules.cert.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.scs.application.core.model.CommonResult;
import com.scs.application.core.model.PageResult;
import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.core.mvc.controller.ImAndExAbleController;
import com.scs.application.core.utils.BeanValidators;
import com.scs.application.core.utils.UserUtils;
import com.scs.application.modules.cert.dto.CertRequirementUpdateDO;
import com.scs.application.modules.cert.entity.CertificateHospitalBusiness;
import com.scs.application.modules.cert.request.CertificateHospitalBusinessQueryRequest;
import com.scs.application.modules.cert.service.CertificateHospitalBusinessService;
import com.scs.application.modules.cert.service.CertMessageService;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 院方业务资质定义 controller
 */
@AllArgsConstructor
@RestController
@RequestMapping("/cert/certificate_hospital_businesss")
public class CertificateHospitalBusinessController extends CrudController<CertificateHospitalBusiness, String, CertificateHospitalBusinessService> implements ImAndExAbleController<CertificateHospitalBusinessQueryRequest> {

    private final CertMessageService certMessageService;

    @GetMapping("/page")
    public PageResult page(CertificateHospitalBusinessQueryRequest request) {
        IPage page = service.page(request);
        return PageResult.ok("查询成功", page.getRecords(), page.getTotal());
    }

    @GetMapping("/list")
    public CommonResult list(CertificateHospitalBusinessQueryRequest request) {
        List<CertificateHospitalBusiness> list = service.list(request);
        return CommonResult.ok("查询成功", list);
    }


    @PutMapping("/batch_save")
    public CommonResult batchSave(@RequestBody @Valid List<CertificateHospitalBusiness> chbs) {
        Assert.notEmpty(chbs, "至少有一条");
        BeanValidators.validateListWithException(validator, chbs);

        String hospitalId = UserUtils.getHospitalId();

        ArrayList<CertificateHospitalBusiness> list = Lists.newArrayList();

        List<CertificateHospitalBusiness> existList = service.list(new CertificateHospitalBusinessQueryRequest().setHospitalId(hospitalId));
        for (CertificateHospitalBusiness chb : chbs) {
            long count = existList.stream().filter(o -> o.getCertificateId().equals(chb.getCertificateId())).count();
            if (count > 0) {
                continue;
            }
            list.add(chb);
        }

        list.stream().forEach(o -> o.setHospitalId(hospitalId));

        if (list.isEmpty()) {
            return CommonResult.ok();
        }
        boolean bSuccess = service.saveOrUpdateBatch(list);

        CertRequirementUpdateDO cru = new CertRequirementUpdateDO();
        cru.setHospitalId(hospitalId);
        certMessageService.certRequirementUpdate(cru);

        return bSuccess ? CommonResult.ok("保存成功") : CommonResult.error("保存失败");
    }

    @Override
    @DeleteMapping({"/{id}"})
    public CommonResult deleteById(@PathVariable("id") String id) {

        CommonResult commonResult = super.deleteById(id);
        CertRequirementUpdateDO cru = new CertRequirementUpdateDO();
        cru.setHospitalId(UserUtils.getHospitalId()).setDelete(true);
        certMessageService.certRequirementUpdate(cru);

        return commonResult;
    }
}
